class_name UnitVisuals
extends Node2D


@onready var sprite: Sprite2D = $Sprite



var _sprite_material: ShaderMaterial
# 受伤闪烁频率
var _hurt_flash_rate: float
# 受伤闪烁持续时间
var _hurt_flash_duration: float
# 受伤闪烁是否启用
var _hurt_flash_enabled: bool
# 受伤闪烁时间
var _hurt_flash_time: float


func _ready():
	_sprite_material = sprite.material as ShaderMaterial


func _process(delta: float):
	if _hurt_flash_enabled:
		if _hurt_flash_time > _hurt_flash_duration:
			stop_hurt_flash()
		_hurt_flash_time += delta



func setup_hurt_flash(flash_rate: float, flash_duration: float):
	_hurt_flash_rate = flash_rate
	_hurt_flash_duration = flash_duration



func start_hurt_flash():
	_hurt_flash_enabled = true
	_hurt_flash_time = 0.0
	_sprite_material.set_shader_parameter("flash_rate", _hurt_flash_rate)


func stop_hurt_flash():
	_hurt_flash_enabled = false
	_sprite_material.set_shader_parameter("flash_rate", 0.0)
